
//
//  OilMgrDetailCell.m
//  carcareIOS
//
//  Created by wr on 16/4/13.
//  Copyright © 2016年 chezheng. All rights reserved.
//

#import "OilMgrDetailCell.h"
#define CELL_HEIGHT (65)
@interface OilMgrDetailCell()

//时间
@property (nonatomic , strong) UILabel * oilTimeL;
//升数
@property (nonatomic , strong) UILabel * oilVolnumeL;
//金额
@property (nonatomic , strong) UILabel * oilMoneyL;

@end

@implementation OilMgrDetailCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        CGFloat labelX = 20 * __MAIN_RATIO_375;
        UIImage * rightImage = ImageNamed(@"cell_right");
        CGFloat labelW = (__MAIN_WIDTH - labelX  - 20 - rightImage.size.width) / 3;
        [Factory imageViewWithCenter: CGPointMake(__MAIN_WIDTH - 20, CELL_HEIGHT / 2) image:rightImage onView:self];
        self.cornerView = [[[UIView viewWithFrame:CGRectMake(0, 0, 7, 7)] resetBackgroundColor:COLOR_HEXSTRING(@"#f83700")] setupOnView:self];
        self.cornerView.layer.masksToBounds = YES;
        self.cornerView.layer.cornerRadius = self.cornerView.width / 2;
        [self.cornerView setCenter:CGPointMake(labelX / 2, CELL_HEIGHT / 2)];
        
        self.oilTimeL = [Factory labelWithFrame:CGRectMake(labelX, 0, labelW, CELL_HEIGHT) font:FONT_DEFAULT_Bold(14) text:@"" textColor:COLOR_HEXSTRING(@"#424242") onView:self textAlignment:NSTextAlignmentLeft];
        self.oilVolnumeL = [Factory labelWithFrame:CGRectMake(self.oilTimeL.right + rightImage.size.width / 2, 0, labelW, CELL_HEIGHT) font:FONT_DEFAULT_Light(20) text:@"" textColor:[Theme wordColor] onView:self textAlignment:NSTextAlignmentCenter];
        self.oilVolnumeL.backgroundColor = [UIColor clearColor];
        self.oilMoneyL = [Factory labelWithFrame:CGRectMake(__MAIN_WIDTH - 20 - rightImage.size.width / 2 - labelW, 0, labelW, CELL_HEIGHT) font:FONT_DEFAULT_Light(20) text:@"" textColor:[Theme wordColor] onView:self textAlignment:NSTextAlignmentRight];
        self.oilMoneyL.backgroundColor = [UIColor clearColor];

        [[[UIView viewWithFrame:CGRectMake(0, CELL_HEIGHT - 0.5, __MAIN_WIDTH, 0.5)] resetBackgroundColor:[Theme lineColor]] setupOnView:self];
    }
    return self;
}
 - (void)setModel:(FuelRecordModel *)model
{
    [self.cornerView setHidden:model.ISREAD];
    self.oilTimeL.text = model.oilMainCdt;
    
    NSString * volnumeStr = [NSString stringWithFormat:@"%0.01f",model.fuelVol];
    NSArray * volnumeStrArr = [volnumeStr componentsSeparatedByString:@"."];
    [self.oilVolnumeL setAttributedText:[self titleWithNameStr:volnumeStrArr[0] withUnitStr:(volnumeStrArr.count > 1 ? [NSString stringWithFormat:@".%@",volnumeStrArr[1]] : @"") withThreeStr:@"升" withUnitFont:FONT_DEFAULT_Light(12) withThreeColor:COLOR_HEXSTRING(@"#9e9e9e") withThreeTextFont:FONT_DEFAULT_Light(14)]];
    
    NSString * moneyStr = [NSString stringWithFormat:@"%0.02f",model.totalPrice];
    NSArray * moneyStrArr = [moneyStr componentsSeparatedByString:@"."];
    [self.oilMoneyL setAttributedText:[self titleWithNameStr:moneyStrArr[0] withUnitStr:(moneyStrArr.count > 1 ? [NSString stringWithFormat:@".%@",moneyStrArr[1]] : @"") withThreeStr:@"元" withUnitFont:FONT_DEFAULT_Light(12) withThreeColor:COLOR_HEXSTRING(@"#9e9e9e") withThreeTextFont:FONT_DEFAULT_Light(14)]];
}

#pragma mark - 生成使用的attribute str
- (NSMutableAttributedString *)titleWithNameStr:(NSString *)nameStr withUnitStr:(NSString *)unitStr withThreeStr:(NSString *)threestr withUnitFont:(UIFont *)font withThreeColor:(UIColor *)color withThreeTextFont:(UIFont *)threeFont
{
    NSString * str = [NSString stringWithFormat:@"%@%@%@",nameStr, unitStr,threestr];
    NSMutableAttributedString * attributeStr = [[NSMutableAttributedString alloc]initWithString:str];
    if([unitStr length] > 0)
    {
        NSRange range = NSMakeRange([str length] - [unitStr length] - [threestr length] , [unitStr length]);
        [attributeStr addAttribute:NSFontAttributeName value:font range:range];
        if(color)
        {
            NSRange colorRange = NSMakeRange([str length] - [threestr length], [threestr length]);
            [attributeStr addAttribute:NSForegroundColorAttributeName value:color range:colorRange];
            [attributeStr addAttribute:NSFontAttributeName value:threeFont range:colorRange];
        }
    }
    return attributeStr;
}
+ (CGFloat)getCellHeight
{
    return CELL_HEIGHT;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
